package cn.wqb.bpp.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.Set;

public class HttpUtils {

    public static String get(String url) {
        RestTemplate rest = new RestTemplate();
        if (url.contains("?")) {
            url += "&random=" + Math.random();
        } else {
            url += "?random=" + Math.random();
        }
        return rest.getForObject(url, String.class);
    }

    /**
     * 没有headerParams可传null
     */
    public static String post(String url, Map<String, Object> params, Map<String, String> headerParams, boolean contentTypeIsJson) {
        RestTemplate rest = new RestTemplate();
        Object httpEntity = null;
        if (contentTypeIsJson) {
            JSONObject json = new JSONObject();
            for (String key : params.keySet()) {
                json.put(key, params.get(key));
            }
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            headers.add("Accept", MediaType.APPLICATION_JSON.toString());
            if (headerParams != null && headerParams.size() > 0) {
                Set<String> keySet = headerParams.keySet();
                for (String key : keySet) {
                    headers.add(key, headerParams.get(key));
                }
            }
            httpEntity = new HttpEntity<String>(json.toString(), headers);
        } else {
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/x-www-form-urlencoded; charset=UTF-8");
            headers.setContentType(type);
            headers.add("Accept", MediaType.APPLICATION_FORM_URLENCODED.toString());
            if (headerParams != null && headerParams.size() > 0) {
                Set<String> keySet = headerParams.keySet();
                for (String key : keySet) {
                    headers.add(key, headerParams.get(key));
                }
            }
            httpEntity = new HttpEntity<>(mapToMultiValueMap(params), headers);
        }
        try {
            if (url.contains("?")) {
                url += "&random=" + Math.random();
            } else {
                url += "?random=" + Math.random();
            }
            ResponseEntity<String> resp = rest.postForEntity(url, httpEntity, String.class);
            return resp.getBody();
        } catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        }
    }

    /**
     * delete
     */
    public static void delete(String url, Map<String, Object> params) {
        RestTemplate rest = new RestTemplate();
        String paramsStr = "";
        for (String key : params.keySet()) {
            if (paramsStr.length() > 0) {
                paramsStr += "&";
            }
            paramsStr += key + "=" + params.get(key);
        }
        rest.delete(url + "?" + paramsStr);
    }

    //Map转换
    private static MultiValueMap<String, Object> mapToMultiValueMap(Map<String, Object> map) {
        MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
        for (String key : map.keySet()) {
            multiValueMap.add(key, map.get(key));
        }
        return multiValueMap;
    }


}
